import java.util.*;

/**
 * @author: gao
 * @create-date: 2025/5/12 15:50
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public TreeNode pruneTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0) {
            root = null;
        }
        return root;
    }

    public static TreeNode BuildTree(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;
        while (!queue.isEmpty() && i < nums.length) {
            TreeNode curNode =  queue.poll();
            if (i < nums.length && nums[i] != -1) {
                curNode.left = new TreeNode(nums[i]);
                queue.offer(curNode.left);
            }
            i++;
            if (i < nums.length && nums[i] != -1) {
                curNode.right = new TreeNode(nums[i]);
                queue.offer(curNode.right);
            }
            i++;
        }
        return root;
    }

    public static void Print(TreeNode root) {
        if (root == null) {
            System.out.print(-1 + " ");
            return;
        }
        System.out.print(root.val + " ");
        Print(root.left);
        Print(root.right);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int len = in.nextInt();
            int[] nums = new int[len];
            for (int i = 0; i < len; i++) {
                nums[i] = in.nextInt();
            }
            TreeNode root = BuildTree(nums);
            Solution solution = new Solution();
            solution.pruneTree(root);
            Print(root);
        }
    }
}